{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G UE of Open Air Interface (OAI)\n",
    "## Objectives\n",
    "- Install and configure a UE \n",
    "- See the UE attached to the previous installed gNB and Core"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At the end of a set of three hands-on (this is the third one), the goal is to run the simulated OAI scenario, which consists of the 5G core (5GC), gNB in CU/DU split mode, and user equipment (UE), according to the image below: \n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy.jpg)\n",
    "\n",
    "Now, we are going to install, configure, deploy and interact to the UE. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## UE Installation\n",
    "\n",
    "If you installed the gNB following the second tutorial, you do not make any addiotional installation.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## UE Configuration\n",
    "\n",
    "**Step 1:** Configure the IMSI, Permanent Key and OPC\n",
    "\n",
    "This part you will configure the UE parameters. The UE parameters file is /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf. The first three lines of the file are composed by the IMSI, Permanent Key and OPC, as shown in figure below. You will configure these parameters. \n",
    "\n",
    "![UE_config_01](../FIGS/H03_5GUE_UNI_III/UE_config_01.png)\n",
    "\n",
    "You need to check in the docker-compose file (/home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml) which SQL file the core is using. See figure below. \n",
    "\n",
    "![UE_config_02](../FIGS/H03_5GUE_UNI_III/UE_config_02.png)\n",
    "\n",
    "\n",
    "Verify/change the parameters in UE file (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf) according to the SQL file illustrated in figure below. The parameters are:\n",
    " - \"ueid\" refers to \"imsi\"\n",
    " - \"encPermanentKey\" refers to \"key\"\n",
    " - \"encOpcKey\" refers to \"opc\n",
    "\n",
    "![UE_config_03](../FIGS/H03_5GUE_UNI_III/UE_config_03.png)\n",
    "\n",
    "To open UE file, type:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf\n",
    "\n",
    "To open Docker Composer file, type:\n",
    "\n",
    "gedit /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml\n",
    "\n",
    "To see sqlFile, type in a terninal.\n",
    "\n",
    "gedit /home/dco1020/oai-cn5g-fed/docker-compose/database/oai_db.sql\n",
    "\n",
    "Or use a SQLLite database visualizer like DB Browser (to install: sudo apt install sqlitebrowser -y).\n",
    "\n",
    "The last three lines of the file are composed of DNN (Data Network Name), N-SSAI SST (Network Slice Selection Assistance Information Slice/Service Type), N-SSAI SD (Network Slice Selection Assistance Information Slice/Service Type). To configure these parameters you will explore the docker-compose file (/home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml), shown in figure below.\n",
    "\n",
    "![UE_config_06](../FIGS/H03_5GUE_UNI_III/UE_config_06.png)\n",
    "\n",
    "The UE parameters (figure below) must be equal to the OAI SMF parameters in the docker-compose file, as illustrated in the figure below.\n",
    " - \"DNN_NI0\" refers to \"dnn\"\n",
    " - \"NSSAI_SST0\" refers to \"nssai_sst\"\n",
    " - \"NSSAI_SD0\" refers to \"nssai_sd\" (the \"NSSAI_SD0\" parameter is in hexadecimal, but in the \"nssai_sd\" parameter you have to use decimal values) \n",
    "\n",
    "![UE_config_04](../FIGS/H03_5GUE_UNI_III/UE_config_04.png)\n",
    "\n",
    "\n",
    "**Note:** In this version of the tutorial, the network slice is limited a just one slice (DNN=oai), because we do not use the NSSF (Network Slice Selection Function).\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## UE Deployment\n",
    "\n",
    "There are some important parameters in the UE deployment command. First, in \"RFSIMULATOR\" the IP 192.168.70.129 is the first valid IP of the subnet configured in the installation of the gNB. The parameters \"-r\", \"--numerology\", \"--band\" and \"-C\" are respectively the number of PRBs, the 5G numerology, the operating band and the frequency of the synchronization block (SSB).  \n",
    "\n",
    "The deployment below is without the OAI UE dashboard, but if you want to deploy the dashboard, follow this tutorial: [OAI UE dashboard](https://github.com/vicentesousa/PPGEEC_2333/blob/main/howtos/H02_5G_OAI_dashboard.ipynb).\n",
    "\n",
    "**Step 1:** to deploy a UE, type in a terminal:\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=192.168.70.129 ./nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --rfsim --sa --nokrnmod -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf\n",
    "\n",
    "**Step 2**: Expected results:\n",
    "\n",
    "To verify that the connection with the UE has been established, make sure that a tunnel for the UE has been created. In a terminal, type:\n",
    "\n",
    "ifconfig --all\n",
    "\n",
    "The expected result is shown in figure below, where an IP address has been assigned to the UE.\n",
    "\n",
    "\n",
    "![UE_config_05](../FIGS/H03_5GUE_UNI_III/UE_config_05.png)\n",
    "\n",
    "\n",
    "You could organize the terminals for all parts of the 5G system and see a screen like the figure below.\n",
    "\n",
    "![5G_final_install](../FIGS/H03_5GUE_UNI_III/final_install.png)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary of Deployment Commands\n",
    "\n",
    "sudo su\n",
    "\n",
    "## 5GC\n",
    "\n",
    "cd /home/dco1020/oai-cn5g-fed/docker-compose\n",
    "\n",
    "sudo python3 core-network.py --type stop-basic --scenario 1\n",
    "\n",
    "sudo python3 core-network.py --type start-basic --scenario 1\n",
    "\n",
    "docker logs -f oai-amf\n",
    "\n",
    "\n",
    "## gNB - CU\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf --continuous-tx --gNBs.[0].min_rxtxtime 6\n",
    "\n",
    "\n",
    "## gNB - DU\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf --continuous-tx --gNBs.[0].min_rxtxtime 6\n",
    "\n",
    "\n",
    "## UE\n",
    "\n",
    "If you want to deploy the OAI dashboard for the UE, follow this tutorial: [OAI UE dashboard](https://github.com/vicentesousa/PPGEEC_2333/blob/main/howtos/H02_5G_OAI_dashboard.ipynb).\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=192.168.70.129 ./nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --rfsim --sa --nokrnmod -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf\n",
    "\n",
    "\n",
    "## Edit configuration files\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf & gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf & gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deliverable 03:\n",
    "- Inspec the CU, DU and AMF logs. Show the UE attachment in those logs.\n",
    "- Produce a unique log file from AMF, cu-gNB and UE. Analyse and highlight the RRC Connection Request from **TASK_RCC_NRUE** creation to **RRCSetupComplete**.\n",
    "    - Tip: Start put AMF logs into a file appending: sudo docker logs oai-amf | ts '%Y%m%d-%H:%M:%S' | sed -E 's/(^[^ ]* [^ ]*)/\\1 [AMF]/' >> log_AMF_CU_DU_UE.txt \n",
    "    - Tip: Start CU-gNB appending: | ts '%Y%m%d-%H:%M:%S' | sed -E 's/(^[^ ]* [^ ]*)/\\1 [CU]/' >> log_AMF_CU_DU_UE.txt\n",
    "    - Tip: Start DU-gNB appending: | ts '%Y%m%d-%H:%M:%S' | sed -E 's/(^[^ ]* [^ ]*)/\\1 [DU]/' >> log_AMF_CU_DU_UE.txt  \n",
    "    - Tip: Start UE appending: | ts '%Y%m%d-%H:%M:%S' | sed -E 's/(^[^ ]* [^ ]*)/\\1 [UE]/' >> log_AMF_CU_DU_UE.txt \n",
    "    - Tip: To sort events by timestamp: sort log_AMF_CU_DU_UE.txt -o log_AMF_CU_DU_UE.txt "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
